﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Looptribe.Moodlin8.Common;
using Looptribe.Moodlin8.ViewModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Looptribe.Moodlin8.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class LoginPage : LayoutAwarePage
    {
        private LoginViewModel ViewModel { get { return (LoginViewModel)DataContext; } }

        public LoginPage()
        {
            this.InitializeComponent();

            // Don't put this page in the navigation history
            ViewModel.LoginCompleted += (sender, args) => Frame.SetNavigationState(App.PrevNav);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (ViewModel.Credentials.Password != null)
                _passwordBox.Password = ViewModel.Credentials.Password;
        }

        private void LoginClick(object sender, RoutedEventArgs e)
        {
            ViewModel.Credentials.Password = _passwordBox.Password;
            ViewModel.Credentials.BaseUri = new Uri(_baseUri.Text);
            if (ViewModel.LoginCommand.CanExecute(null))
                ViewModel.LoginCommand.Execute(null);
        }
    }
}
